import javax.swing.tree.TreeNode;
import java.util.LinkedList;
import java.util.List;
import java.util.*;


/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2025-02-22
 * Time: 12:53
 */
public class test2 {
    /**
     * LeetCode 515  在每个树行中找最大值
     */
    public List<Integer> largestValues(TreeNode root) {
        List<Integer> ret = new ArrayList<>();
        if(root==null){
            return ret;
        }
        Queue<TreeNode> q = new LinkedList<>();
        q.add(root);
        while(!q.isEmpty()){
            int size = q.size();
            int tmp = Integer.MIN_VALUE;
            for(int i=0;i<size;i++){
                TreeNode t = q.poll();
                tmp = Math.max(tmp,t.val);
                if(t.left!=null){
                    q.add(t.left);
                }
                if(t.right!=null){
                    q.add(t.right);
                }
            }
            ret.add(tmp);
        }
        return ret;
    }


    public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
}
